Namespaces
Variants
Views
Actions

Variadic arguments

From cppreference.com
< cpp‎ | language
 
 
C++ language
General topics
Flow control
Conditional execution statements
Iteration statements
Jump statements
Functions
function declaration
lambda function declaration
function template
inline specifier
exception specifications (deprecated)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
decltype specifier (C++11)
Specifiers
cv specifiers
storage duration specifiers
constexpr specifier (C++11)
auto specifier (C++11)
alignas specifier (C++11)
Initialization
Literals
Expressions
alternative representations
Utilities
Types
typedef declaration
type alias declaration (C++11)
attributes (C++11)
Casts
implicit conversions
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-style and functional cast
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
class template
function template
template specialization
parameter packs (C++11)
Miscellaneous
Inline assembly
 

Allows a function to accept any number of arguments.

Indicated by the parameter of the form ... which must appear last in the parameter-list of a function declaration.

Where syntactically correct, , ... may be replaced by ....

// the function delared as follows
int printx(const char* fmt, ...);
// may be called with one or more arguments:
printx("hello world");
printx("a=%d b=%d", a, b);
 
int printx(const char* fmt...); // same as above (comma is optional)
int printy(..., const char* fmt); // error: ... must be the last
int printz(...); // valid, but the arguments cannot be accessed portably

Note: this is different from a function parameter pack expansion, which is indicated by an ellipsis that is a part of a parameter declarator, rather than an ellipsis that appears as a parameter on its own. Both parameter pack expansion and the ellipsis parameter may appear in the declaration of a function template, as in the case of std::is_function.

[edit] Default conversions

When a variadic function is called, after lvalue-to-rvalue, array-to-pointer, and function-to-pointer conversions, each argument that is a part of the variable argument list undergoes additional conversions known as default argument promotions:

Only arithmetic, enumeration, pointer, pointer to member, and class type arguments are allowed.

Within the body of a function that uses variadic arguments, the values of these arguments may be accessed using the <cstdarg> library facilities:

Defined in header <cstdarg>
enables access to variadic function arguments
(function macro) [edit]
accesses the next variadic function argument
(function macro) [edit]
(C++11)
makes a copy of the variadic function arguments
(function macro) [edit]
ends traversal of the variadic function arguments
(function macro) [edit]
holds the information needed by va_start, va_arg, va_end, and va_copy
(class) [edit]

[edit] Alternatives

  • Variadic templates can also be used to create functions that take variable number of arguments. They are often the better choice because they do not impose restrictions on the types of the arguments, do not perform integral and floating-point promotions, and are type safe. (since C++11)
  • If all variable arguments share a common type, a std::initializer_list provides a convenient mechanism (albeit with a different syntax) for accessing variable arguments.

[edit] Notes

In the C programming language, at least one named parameter must appear before the ellipsis parameter, so printz(...); is not valid.